package com.wd.health.view.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;


import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.wd.health.R;

import com.wd.health.model.adapter.VideoAdapter;
import com.wd.health.model.adapter.VideoFlAdapter;
import com.wd.health.model.bean.BarrageBean;
import com.wd.health.model.bean.HealthJtBean;
import com.wd.health.model.bean.VideoInfoBean;

import com.wd.health.utils.DpTools;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.ScrollCalculatorHelper;
import com.wd.health.view.widget.BarrageView;


import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class VideoFragment extends Fragment{
    //弹幕
    private BarrageView video_fragment_barrageview;

    private RecyclerView rv_video_01;
    private RecyclerView rv_video_02;
    private int mid = 1;
    //控制滚动播放
    ScrollCalculatorHelper scrollCalculatorHelper;

    private VideoAdapter videoAdapter;

    private int pricee;//购买回调 价格
    private int videoIdd;//购买回调 视频id
    private int positionn;//购买回调 当前索引

    //输入框布局
    private LinearLayout video_fragment_input_layout;
    //输入框
    private EditText video_fragment_input;
    //发送评论
    private ImageView video_fragment_send;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_video, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        rv_video_01 = view.findViewById(R.id.rv_video_01);
        rv_video_02 = view.findViewById(R.id.rv_video_02);
        //初始化弹幕控件
        video_fragment_barrageview=view.findViewById(R.id.video_fragment_barrageview);
        //初始化输入框布局
        video_fragment_input_layout=view.findViewById(R.id.video_fragment_input_layout);
        //输入框
        video_fragment_input=view.findViewById(R.id.video_fragment_input);
        //发送
        video_fragment_send=view.findViewById(R.id.video_fragment_send);

        rv_video_01.bringToFront();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false);
        rv_video_01.setLayoutManager(linearLayoutManager);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        //获取屏幕宽高
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        //自定播放帮助类 限定范围为屏幕一半的上下偏移180   括号里不用在意 因为是一个item一个屏幕
        scrollCalculatorHelper = new ScrollCalculatorHelper(R.id.list_video_player
                , dm.heightPixels / 2 - DpTools.dip2px(getContext(), 180)
                , dm.heightPixels / 2 + DpTools.dip2px(getContext(), 180));

        //让RecyclerView有ViewPager的翻页效果
        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(rv_video_02);
        //设置LayoutManager和Adapter
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rv_video_02.setLayoutManager(linearLayoutManager);
        initRV01();

        //设置滑动监听
        rv_video_02.addOnScrollListener(new RecyclerView.OnScrollListener() {
            //第一个可见视图,最后一个可见视图
            int firstVisibleItem, lastVisibleItem;

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                //如果newState的状态==RecyclerView.SCROLL_STATE_IDLE;
                //播放对应的视频
                scrollCalculatorHelper.onScrollStateChanged(recyclerView, newState);

                video_fragment_barrageview.setItemText(null,false);// 取消弹幕

            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                Log.e("有几个item", firstVisibleItem + "    " + lastVisibleItem);
                //一屏幕显示一个item 所以固定1
                //实时获取设置 当前显示的GSYBaseVideoPlayer的下标
                scrollCalculatorHelper.onScroll(recyclerView, firstVisibleItem, lastVisibleItem, 1);

                video_fragment_barrageview.setItemText(null,false);// 取消弹幕
            }

        });
    }


    private void initRV01() {
        RetrofitManager.getInstance().getHealthJt(new Observer<HealthJtBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(HealthJtBean healthJtBean) {
                List<HealthJtBean.ResultBean> list = healthJtBean.getResult();
                final VideoFlAdapter videoFlAdapter = new VideoFlAdapter(list, getContext());
                rv_video_01.setAdapter(videoFlAdapter);
                videoFlAdapter.setGetListener(new VideoFlAdapter.GetListener() {
                    @Override
                    public void onClick(int id, int position) {
                        videoFlAdapter.setmPosition(position-1);
                        mid = id;
                        initRv02();
                        videoFlAdapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
    private void initRv02() {
        RetrofitManager.getInstance().getVideoInfoData(mid+2, 1, 10, new Observer<VideoInfoBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(VideoInfoBean videoInfoBean) {
                List<VideoInfoBean.ResultBean> result = videoInfoBean.getResult();
                videoAdapter = new VideoAdapter(result,getContext());
                rv_video_02.setAdapter(videoAdapter);
                videoAdapter.setStartBarrage(new VideoAdapter.StartBarrage() {
                    @Override
                    public void setStartBarrage(int videoId) {
                        initBarrage(videoId);
                    }
                });
                videoAdapter.setStopBarrage(new VideoAdapter.StopBarrage() {
                    @Override
                    public void stopBarrage() {
                        video_fragment_barrageview.setItemText(null,false);
                        video_fragment_barrageview.setVisibility(View.GONE);
                    }
                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initBarrage(int videoId) {
        RetrofitManager.getInstance().getBarrageData(videoId, new Observer<BarrageBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BarrageBean barrageBean) {
                List<BarrageBean.ResultBean> result = barrageBean.getResult();
                video_fragment_barrageview.setVisibility(View.VISIBLE);
                video_fragment_barrageview.setItemText(result,true);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {


        Configuration mConfiguration = this.getResources().getConfiguration();
        int ori = mConfiguration.orientation;
        if (ori == Configuration.ORIENTATION_LANDSCAPE) {


            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏

        } else if (ori == Configuration.ORIENTATION_PORTRAIT) {
            //当前为竖屏
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //显示状态栏
        }

        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }

}